//
//  main.swift
//  OneMeeting
//
//  Created by zhangheng on 2019/8/8.
//  Copyright © 2019 www.zhangheng.com. All rights reserved.
//

import Cocoa

func mainMenu() -> NSMenu {
    
    let    mainMenu             =    NSMenu()
    let    mainAppMenuItem      =    NSMenuItem(title: "OneMeeting", action: nil, keyEquivalent: "")
    let    mainFileMenuItem     =    NSMenuItem(title: "会议", action: nil, keyEquivalent: "")
    mainMenu.addItem(mainAppMenuItem)
    mainMenu.addItem(mainFileMenuItem)
    
    let    appMenu              =    NSMenu()
    mainAppMenuItem.submenu     =    appMenu
    
    let    appServicesMenu      =    NSMenu()
    NSApp.servicesMenu          =    appServicesMenu
    appMenu.addItem(withTitle: "关于 OneMeeting", action: #selector(AppDelegate.about), keyEquivalent: "")
    appMenu.addItem(withTitle: "检查更新", action: #selector(AppDelegate.checkVersion), keyEquivalent: "")
    appMenu.addItem(NSMenuItem.separator())
    appMenu.addItem(withTitle: "偏好设置", action: #selector(AppDelegate.clickPreferences), keyEquivalent: ",")
    
    appMenu.addItem(NSMenuItem.separator())
    appMenu.addItem(withTitle: "隐藏当前", action: #selector(NSApplication.hide(_:)), keyEquivalent: "h")
    appMenu.addItem({ ()->NSMenuItem in
        let m = NSMenuItem(title: "隐藏其他", action: #selector(NSApplication.hideOtherApplications(_:)), keyEquivalent: "h")
        m.keyEquivalentModifierMask = NSEvent.ModifierFlags([.command, .option])
        m.menu?.autoenablesItems = true
        m.isEnabled = true
        return m
        }())
    appMenu.addItem(withTitle: "全部显示", action: #selector(NSApplication.unhideAllApplications(_:)), keyEquivalent: "")
    
    appMenu.addItem(NSMenuItem.separator())
    appMenu.addItem(withTitle: "服务", action: nil, keyEquivalent: "").submenu    =    appServicesMenu
    appMenu.addItem(NSMenuItem.separator())
    appMenu.addItem(withTitle: "退出", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")
    
    let    fileMenu             =    NSMenu(title: "会议")
    mainFileMenuItem.submenu    =    fileMenu
    fileMenu.addItem(withTitle: "召开新会议", action: #selector(NSDocumentController.newDocument(_:)), keyEquivalent: "k")
    fileMenu.addItem(withTitle: "加入会议", action: #selector(NSDocumentController.newDocument(_:)), keyEquivalent: "g")
    fileMenu.addItem(withTitle: "结束会议", action: #selector(NSDocumentController.newDocument(_:)), keyEquivalent: "d")
    
    return mainMenu
}

autoreleasepool {
    let app      = NSApplication.shared//创建应用
    let delegate = AppDelegate()
    app.delegate = delegate//配置应用代理
    app.mainMenu = mainMenu()//配置菜单，mainMenu 函数需要前向定义，否则编译错误
    app.run() //启动应用
}
